#include "TDownloadDialog.h"

#include "TMessageBox.h"
#include <QDesktopServices>
#include <QVBoxLayout>
#include <QPushButton>
#include <QFileDialog>
#include <QUrl>
#include <QApplication>
#include <QProgressBar>

PreDownUi::PreDownUi(QWidget *parent)
    :QWidget(parent)
{
    resize(300, 180);
    verticalLayout = new QVBoxLayout(this);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    verticalSpacer = new QSpacerItem(20, 16, QSizePolicy::Minimum, QSizePolicy::Expanding);

    verticalLayout->addItem(verticalSpacer);

    pbtOpen = new QPushButton(this);
    pbtOpen->setObjectName(QString::fromUtf8("pbtOpen"));
    pbtOpen->setFixedHeight(25);
    pbtOpen->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    verticalLayout->addWidget(pbtOpen);

    verticalSpacer_2 = new QSpacerItem(20, 17, QSizePolicy::Minimum, QSizePolicy::Expanding);

    verticalLayout->addItem(verticalSpacer_2);

    pbtSaveAs = new QPushButton(this);
    pbtSaveAs->setObjectName(QString::fromUtf8("pbtSaveAs"));
    pbtSaveAs->setFixedHeight(25);
    pbtSaveAs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    verticalLayout->addWidget(pbtSaveAs);

    verticalSpacer_3 = new QSpacerItem(20, 16, QSizePolicy::Minimum, QSizePolicy::Expanding);

    verticalLayout->addItem(verticalSpacer_3);

    pbtOpen->setText(QApplication::translate("PreDownUi", "\347\233\264\346\216\245\346\211\223\345\274\200", 0, QApplication::UnicodeUTF8));
    pbtSaveAs->setText(QApplication::translate("TPreDownloadDialog", "\345\217\246\345\255\230\344\270\272", 0, QApplication::UnicodeUTF8));
}

DownUi::DownUi(QWidget *parent)
    :QWidget(parent)
{
    resize(300, 180);
    verticalLayout = new QVBoxLayout(this);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    verticalSpacer = new QSpacerItem(20, 64, QSizePolicy::Minimum, QSizePolicy::Expanding);

    verticalLayout->addItem(verticalSpacer);

    label = new QLabel(this);
    label->setObjectName(QString::fromUtf8("label"));
    label->setMinimumSize(QSize(0, 25));
    label->setAlignment(Qt::AlignCenter);

    verticalLayout->addWidget(label);

    progressBar = new QProgressBar(this);
    progressBar->setObjectName(QString::fromUtf8("progressBar"));
    progressBar->setFixedHeight(25);
    progressBar->setTextVisible(false);
    progressBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    verticalLayout->addWidget(progressBar);

    horizontalLayout = new QHBoxLayout();
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout->addItem(horizontalSpacer);

    pbtCancel = new QPushButton(this);
    pbtCancel->setObjectName(QString::fromUtf8("pbtCancel"));
    pbtCancel->setMinimumSize(QSize(0, 25));

    horizontalLayout->addWidget(pbtCancel);

    verticalLayout->addLayout(horizontalLayout);
    label->setText(QApplication::translate("TDownloadingDialog", "\346\226\207\344\273\266\344\270\213\350\275\275\344\270\255", 0, QApplication::UnicodeUTF8));
    pbtCancel->setText(QApplication::translate("TDownloadingDialog", "\345\217\226\346\266\210", 0, QApplication::UnicodeUTF8));
}

TDownloadDialog::TDownloadDialog(TDocumentPointer pDoc, QWidget *parent)
    :TSkinDialog(parent)
{
    this->setMinimumSize(300, 200);
    _pDoc = pDoc;

    QVBoxLayout* vlayout = new QVBoxLayout(getClient());
    vlayout->setContentsMargins(0,0,0,0);
    vlayout->setSpacing(0);
    _wPreDown = new PreDownUi();
    _wDown = new DownUi();
    vlayout->addWidget(_wPreDown);
    vlayout->addWidget(_wDown);
    _wDown->hide();

    _requester = TContext::instance()->requesterFactory()->createDownloadRequester();
    connect(_requester.data(), SIGNAL(downloadFinished(bool, QString)),
            this, SLOT(downloadFinished(bool,QString)));
    connect(_requester.data(), SIGNAL(downloadProgress(qint64, qint64)),
            this, SLOT(downloadProgress(qint64, qint64)));

    connect(_wPreDown->pbtOpen, SIGNAL(clicked()), this, SLOT(doDownload()));
    connect(_wPreDown->pbtSaveAs, SIGNAL(clicked()), this, SLOT(doDownload()));
}

void TDownloadDialog::doDownload()
{
    if(sender() == _wPreDown->pbtOpen)
    {
        _mode = DirectOpen;
    }
    else if(sender() == _wPreDown->pbtSaveAs)
    {
        _mode = SaveAs;
    }

    //change ui
    _wPreDown->hide();
    _wDown->show();


    //requet
    _wDown->progressBar->setValue(10000);
    if(DirectOpen == _mode && !_pDoc.isNull())
    {
        _requester->download(_pDoc->getId());
    }
    else if (SaveAs == _mode && !_pDoc.isNull())
    {
        QString saveDir = QFileDialog::getSaveFileName(this,
                                                       tr("Save File"), "./" + _pDoc->getFilename(),
                                                       "");

        if(!saveDir.isEmpty())
            _requester->download(_pDoc->getId(), saveDir);
        else
        {
            //change ui
            _wDown->hide();
            _wPreDown->show();
        }
    }
}

void TDownloadDialog::downloadFinished(bool error, QString localFilename)
{
    if(error)
    {
        TMessageBox dialog(tr("caution"),
                           tr("download failed."),
                           QMessageBox::Ok,
                           QMessageBox::NoButton, this);

        dialog.centerWindow();
        dialog.exec();
    }
    else
    {
        if(DirectOpen == _mode)
        {
            QDesktopServices::openUrl(QUrl(localFilename));
        }
        else
        {
            TMessageBox dialog(tr("prompt"),
                               tr("download finished."),
                               QMessageBox::Ok,
                               QMessageBox::NoButton, this);

            dialog.centerWindow();
            dialog.exec();
        }
    }

    this->close();
}

void TDownloadDialog::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
    _wDown->progressBar->setValue((int)(bytesReceived/(bytesTotal * 1.0))*10000);
}

